// course.js
const courseApp = createApp({
    data() {
        return {
            courses: [],
            colleges: [],
            loading: false,
            error: null,
            formData: {
                courseCode: '',    // 课程编号
                courseName: '',    // 课程名称
                credit: '',        // 学分
                collegeCode: ''    // 开课学院代码
            }
        }
    },
    methods: {
        // 加载课程数据
        async loadCourses() {
            this.loading = true;
            this.error = null;
            try {
                const response = await fetch(window.contextPath+'/course/listAll');
                const collegeResponse = await fetch(window.contextPath+'/college/listAll');
                if (response.ok && collegeResponse.ok) {
                    this.courses = await response.json();
                    this.colleges = await collegeResponse.json();
                } else {
                    const errorText = await response.text();
                    this.error = '加载数据失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            }catch ( error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }finally {
                this.loading = false;
            }
        },
        // 添加课程信息
        async addCourse() {
            try {
                const response = await fetch(window.contextPath+'/course/add', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(this.formData)
                });

                if (response.ok) {
                    alert('添加成功')
                    await this.loadCourses();
                    this.formData = {
                        courseCode: '',    // 课程编号
                        courseName: '',    // 课程名称
                        credit: '',        // 学分
                        collegeCode: ''    // 开课学院代码
                    };
                    closeCourseAddModal();
                } else {
                    const errorText = await response.text();
                    this.error = '添加课程信息失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 删除课程信息
        async deleteCourse(courseId) {
            try {
                if (!confirm('确定要删除该课程信息吗？')) {
                    return;
                }
                const response = await fetch(window.contextPath+'/course/delete?courseId=' + courseId, {
                    method: 'DELETE'
                });
                if (response.ok) {
                    alert('删除成功')
                    await this.loadCourses();
                } else {
                    const errorText = await response.text();
                    this.error = '删除课程失败,错误码:'+ response.status +',错误信息: ' + errorText
                    showErrorModal(this.error)
                }
            } catch (error) {
                this.error = '网络错误: ' + error.message;
                showErrorModal(this.error)
            }
        },
        // 修改课程信息
        async updateCourse() {
            alert('该功能正在开发中...')
        },
        // 根据学院代码获取学院名称
        getCollegeNameByCode(collegeCode) {
            const college = this.colleges.find(college => college.collegeCode === collegeCode);
            return college ? college.collegeName : '未知学院';
        }
    },
    mounted() {
        window.courseAppInstance = this;
    }
});
courseApp.mount('#course-content');

// 显示添加课程表单
function showCourseAddModal() {
    const modal = document.getElementById('courseAddModal');
    modal.classList.add('show');
    modal.style.display = 'flex';
}

// 关闭添加课程表单
function closeCourseAddModal() {
    document.getElementById('courseAddModal').style.display = 'none';
}
